﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Modules.jForm
{
    public class jFormModule : Module
    {
        public static Dictionary<string, string> AvailableForms
        {
            get
            {
                Dictionary<string, string> d = new Dictionary<string, string>();
                d.Add("OpenQuarters.WebQuarters.Modules.jForm,Resources.Default.tpl", "[Default]");

                foreach (string path in System.IO.Directory.GetFiles(Oracle.Request.MapPath("~/Forms/"), "*.tpl"))
                {
                    d.Add(System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetFileName(path));
                }

                return d;
            }
        }

        private string form = "OpenQuarters.WebQuarters.Modules.jForm,Resources.Default.tpl";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.jForm.jFormModule.AvailableForms")]
        public string Form
        {
            get { return form; }
            set { form = value; }
        }

        public enum PostMethod
        {
            Get,
            Post
        }

        private PostMethod postMethod = PostMethod.Post;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown)]
        public PostMethod Post_Method
        {
            get { return postMethod; }
            set { postMethod = value; }
        }

        private string postAction = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Post_Action
        {
            get { return postAction; }
            set { postAction = value; }
        }

        private bool useJavaScript = true;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.CheckBox)]
        public Boolean Use_Javascript
        {
            get { return useJavaScript; }
            set { useJavaScript = value; }
        }

        private string onSuccessScript = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string On_Success_Script
        {
            get { return onSuccessScript; }
            set { onSuccessScript = value; }
        }

        public override void Initialise()
        {
            this.ModuleName = "jForm";
            this.ModuleDescription = "Places an jForm on the page";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (form.IndexOf(",") == -1 && System.IO.File.Exists(Server.MapPath("~/Forms/" + form + ".tpl")))
            {
                this.Controls.Add(new JForm(form, postAction)
                {
                    UseJavaScript = useJavaScript,
                    OnSuccessScript = onSuccessScript
                });
            }
            else
            {
                this.Controls.Add(new JForm(form, JForm.TemplateType.EmbeddedResource, postAction)
                {
                    UseJavaScript = useJavaScript,
                    OnSuccessScript = onSuccessScript
                });

            }
        }
    }
}
